﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetalBase.Base;
using NetalBase.Reflection;
using System.Diagnostics.Contracts;
using RmProto.Automata.CRA;
using System.Diagnostics;

namespace RmProto.Automata.IA
{
	public class Interpretation :
		AutomatonBase<IA, IAEventPoint, IADataPoint>,
		IA,
		IAEventPoint
	{
		#region States
		readonly Ic Ic;
		readonly Id Id;
		readonly If If;
		readonly Ir Ir;
		readonly Ip Ip;
		readonly Ie Ie;
		readonly Ifin Ifin;
		#endregion

		#region IA Members
		public virtual void e_I_d(object data)
		{
			s_s(Ic, Id);
			s_s(If, Ir);
		}

		public virtual void e_I_f(Action<object> func)
		{
			s_s(Ic, If);
			s_s(Id, Ir);
		}

		public virtual void e_I_p(ICRA cr)
		{
			s_s(Ir, Ip);
		}

		public virtual void e_I_fin(object result)
		{
			s_s(Ip, Ifin);
		}

		public virtual void e_I_e(string errorMessage)
		{
			s_s(Ip, Ie);
		}

		public object Data { get { return DataPoint.Data; } }

		public Action<object> Func { get { return DataPoint.Func; } }

		#endregion

		#region Public methods (auxillary)
		public Interpretation()
		{
			Contract.Ensures(InState<Ic>());

			Ic = createInitState<Ic>();
			Id = createState<Id>();
			If = createState<If>();
			Ir = createState<Ir>();
			Ip = createState<Ip>();
			Ie = createFinalState<Ie>();
			Ifin = createFinalState<Ifin>();
		}

		[Pure]
		public static bool InState<TState>(IA i) where TState : StateBase<IAEventPoint, IADataPoint>
		{
			return (i as AutomatonBase<IA, IAEventPoint, IADataPoint>).InState<TState>();
		}
		#endregion
	}
}
